package com.zjdiepu.www.manage.api.dh;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.ArchiveStatus;
import com.zjdiepu.www.base.enums.status.BusiStatus;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessVoMapper;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.dh.IDhInsuranceService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.dh.dto.DhInsurance;
import com.zjdiepu.www.model.dh.ext.DhInsuranceExt;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;

/**
 * 保险Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月17日 10时36分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "DhInsuranceController", produces="application/json;charset=UTF-8")
@Api(tags = {"保险"}, produces="application/json;charset=UTF-8")
public class DhInsuranceController extends BaseController {
    @Resource
    private IDhInsuranceService dhInsuranceService;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private YwBusinessVoMapper ywBusinessVoMapper;
    @Resource
    private ICachedDictionaryService cachedDictionaryService;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: insurance  <br/>
     * @Description: 保险单录入<br/>
     * @param insurance
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "保险单不保", response = Boolean.class, httpMethod="POST", tags={"保险单不保"})
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "renewalId", value = "保险单号", paramType="query", required = true, example="0"),
    	@ApiImplicitParam(name = "isRenewal", value = "是否为续保", paramType="query", required = false, example="0")
    })
    @RequestMapping(value = "cancelInsurance", method = RequestMethod.POST)
    public ApiResponse cancelInsurance(String renewalId, @RequestParam(defaultValue="0")boolean isRenewal) {
    	if(StringUtils.isBlank(renewalId)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	return dhInsuranceService.saveCancelInsurance(renewalId, isRenewal, currentUser);
    }
    /**
     * @author xiongxiaotun <br/>
     * @Title: insurance  <br/>
     * @Description: 保险单录入<br/>
     * @param insurance
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "保险单录入", response = Boolean.class, httpMethod="POST", tags={"保险单录入"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "isRenewal", value = "是否为续保", paramType="query", required = false, example="0")
    })
    @RequestMapping(value = "insurance", method = RequestMethod.POST)
    public ApiResponse insurance(@ModelAttribute DhInsurance dhInsurance, @RequestParam(defaultValue="0") boolean isRenewal) {
    	DhInsurance dhInsurance_ = new DhInsurance();
    	dhInsurance_.setYear(dhInsurance.getYear());
    	dhInsurance_.setBusiId(dhInsurance.getBusiId());
    	dhInsurance_.setId(dhInsurance.getId());
    	boolean repeat = dhInsuranceService.repeat(dhInsurance_);
    	if(repeat) {
    		ApiResponse.fail(999, "该年度保单已经录入,请确认重新选择年度");
    	}
    	AbstractSysUser currentUser = getCurrentUser();
        return dhInsuranceService.saveInsurance(dhInsurance, isRenewal, currentUser);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: save  <br/>
     * @Description: 新增保险 <br/>
     * @param dhInsurance
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增保险", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute DhInsurance dhInsurance) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dhInsurance.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dhInsurance.setModifierName(currentUser.getName());
        dhInsuranceService.saveUpdateAndDelete(dhInsurance, OperateType.add);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: remove  <br/>
     * @Description: 删除保险<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除保险", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhInsuranceService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: disable  <br/>
     * @Description: 禁用保险<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用保险", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhInsuranceService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: enable <br/>
     * @Description: 启用保险<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用保险", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dhInsuranceService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: modify  <br/>
     * @Description: 修改保险 <br/>
     * @param dhInsurance
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改保险", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute DhInsurance dhInsurance) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dhInsurance.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dhInsurance.setModifierName(currentUser.getName());
    	dhInsuranceService.saveUpdateAndDelete(dhInsurance, OperateType.edit);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询保险<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询保险", response = DhInsurance.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       DhInsurance dhInsurance = dhInsuranceService.selectById(id);
       if(null == dhInsurance) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(dhInsurance);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 条件查询保险 <br/>
     * @param dhInsuranceExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询保险",response = DhInsurance.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute DhInsuranceExt dhInsuranceExt){
        dhInsuranceExt.notDeleted();
        PageVo page = getPage(dhInsuranceExt);
        int records = dhInsuranceService.countByExt(dhInsuranceExt);
        page.setRecords(records);
        if (records > 0) {
            List<DhInsurance> dhInsurances = dhInsuranceService.selectByExt(dhInsuranceExt);
            for (DhInsurance dhInsurance : dhInsurances) {
            	dhInsurance.setCompanyInsuranceDisplay(cachedDictionaryService.getName(String.valueOf(dhInsurance.getCompanyInsurance()), DataDicType.INSURANCE_COMPANY));
            	dhInsurance.setOperatorNextDisplay(cachedDictionaryService.getName(String.valueOf(dhInsurance.getOperatorNext()), DataDicType.INSURANCE_EMPLOYEE));
			}
            page.setRows(dhInsurances);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

    @ApiOperation(value = "条件查询业务信息",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "multiList", method = RequestMethod.POST)
    public ApiResponse multiList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser currentUser = (SysUser)getCurrentUser();
        PageVo page = getPage(ywBusinessQueryRequest);
        ywBusinessQueryRequest.setBankLoan(currentUser.getLoanBank());
        ywBusinessQueryRequest.setCodeUnits( currentUser.getCodeUnits());
        ywBusinessQueryRequest.setBusiStatusMore(BusiStatus.YIN_HANG_YI_FANG_KUAN.getCode());
        ywBusinessQueryRequest.setBusiStatusLess(BusiStatus.SHEN_PI_WAN_CHENG.getCode());
        ywBusinessQueryRequest.setStatusArchive(ArchiveStatus.UN_ARCHIVE.getCode());
        int records = ywBusinessService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<YwBusiness> ywBusinesss = ywBusinessService.selectMultiByExt(ywBusinessQueryRequest);
            DhInsurance dhInsurance=null;
            for (YwBusiness ywBusiness : ywBusinesss) {
            	dhInsurance=new DhInsurance();
            	dhInsurance.setBusiId(ywBusiness.getId());
            	dhInsurance=dhInsuranceService.selectRefined(dhInsurance);
            	if(dhInsurance!=null){
            		ywBusiness.setDateStart(dhInsurance.getDateStart());
            		ywBusiness.setDateEnd(dhInsurance.getDateEnd());
            		ywBusiness.setInsureListId(dhInsurance.getId());
            	}
			}
            ywBusinessVoMapper.mapper(ywBusinesss);
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
}
